﻿/*  Eleven Business Suite : Un ERP básico para pequeños empresarios.
 *  Copyright (C) 2011  Francis Bolaños Arias
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>;.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ElevenCore;
using ElevenGlobal;

namespace ElevenGUI
{
    public partial class FrmInventario : Form
    {

        private bool Ascedente = false;

        public FrmInventario()
        {
            InitializeComponent();
        }

        private void cargarInventario() {
            dgvInventario.Rows.Clear();
            String Ordenar = "IdProducto";
            switch (cbOrdenar.SelectedIndex) { 
                case 0:
                    Ordenar = "IdProducto";
                    break;
                case 1:
                    Ordenar = "Nombre";
                    break;
                case 2:
                    Ordenar = "Descripcion";
                    break;
                case 3:
                    Ordenar = "Cantidad";
                    break;
                case 4:
                    Ordenar = "PrecioUnitario";
                    break;
                case 5:
                    Ordenar = "PrecioVenta";
                    break;
                case 6:
                    Ordenar = "TotalImpuesto";
                    break;
                case 7:
                    Ordenar = "Ganancia";
                    break;
                case 8:
                    Ordenar = "GananciaIVI";
                    break;
                case 9:
                    Ordenar = "TotalOrdenado";
                    break;
                case 10:
                    Ordenar = "InversionReal";
                    break;
                case 11:
                    Ordenar = "InversionTotal";
                    break;
                case 12:
                    Ordenar = "TotalVendido";
                    break;
                case 13:
                    Ordenar = "TotalIngreso";
                    break;
                case 14:
                    Ordenar = "IdCodigoBarras";
                    break;
            }
            DataTable dt;
            if (rbGlobal.Checked)
            {
                dt = SCMInventario.consultar(0 + (int.Parse(txtPagina.Text) * 20), 20 + (int.Parse(txtPagina.Text) * 20)
                , Ordenar, Ascedente);
            }
            else{
                dt = SCMInventario.consultarXMes(cbMes.SelectedIndex, Convert.ToInt32(nudAnno.Value), 0 + (int.Parse(txtPagina.Text) * 20), 20 + (int.Parse(txtPagina.Text) * 20)
                    , Ordenar, Ascedente);
            }
            dgvInventario.Rows.Clear();
            foreach (DataRow dr in dt.Rows) {
                string ganancia = String.Format("{0:0.00}", dr["Ganancia"]) + "%";
                if(ganancia.Equals("%")){
                    ganancia = "";
                }
                string gananciaIVI = String.Format("{0:0.00}", dr["GananciaIVI"]) + "%";
                if (gananciaIVI.Equals("%"))
                {
                    gananciaIVI = "";
                }
                dgvInventario.Rows.Add(dr["IdProducto"], dr["Nombre"], dr["Descripcion"]
                    , dr["Cantidad"], dr["PrecioUnitario"], dr["PrecioVenta"]
                    , String.Format("{0:0.00}", dr["Impuesto"]) + "%", ganancia
                    , gananciaIVI, dr["TotalOrdenado"], String.Format("{0:0.00}", dr["InversionReal"])
                    , String.Format("{0:0.00}", dr["InversionTotal"]), dr["TotalVendido"]
                    , String.Format("{0:0.00}", dr["TotalIngreso"]), dr["IdCodigoBarras"]);
            }
        }

        private void FrmInventario_Load(object sender, EventArgs e)
        {
            cbOrdenar.SelectedIndex = 0;
            cbMes.SelectedIndex = DateTime.Now.Month - 1;
            nudAnno.Value = DateTime.Now.Year;
        }

        private void btnInicio_Click(object sender, EventArgs e)
        {
            txtPagina.Text = "0";
            cargarInventario();
        }

        private void rbMensual_CheckedChanged(object sender, EventArgs e)
        {
            cargarInventario();
        }

        private void rbGlobal_CheckedChanged(object sender, EventArgs e)
        {
            cargarInventario();
        }

        private void cbMes_SelectedIndexChanged(object sender, EventArgs e)
        {
            rbMensual.Checked = true;
            cargarInventario();
        }

        private void btnRecargar_Click(object sender, EventArgs e)
        {
            cargarInventario();
        }

        private void btnDesc_Click(object sender, EventArgs e)
        {
            Ascedente = false;
            cargarInventario();
        }

        private void btnAtras_Click(object sender, EventArgs e)
        {
            int pagina = int.Parse(txtPagina.Text);
            if (pagina > 0)
            {
                txtPagina.Text = Convert.ToString(pagina - 1);
                cargarInventario();
            }
        }

        private void btnAdelante_Click(object sender, EventArgs e)
        {
            txtPagina.Text = Convert.ToString(int.Parse(txtPagina.Text) + 1);
            cargarInventario();
        }

        private void btnAsc_Click(object sender, EventArgs e)
        {
            Ascedente = true;
            cargarInventario();
        }

        private void cbOrdenar_SelectedIndexChanged(object sender, EventArgs e)
        {
            cargarInventario();
        }

        private void nudAnno_ValueChanged(object sender, EventArgs e)
        {
            cargarInventario();
        }
    }
}
